﻿using Global;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using ProdService;

namespace UI.Filters
{
    public class NeedLogon : Attribute, IAuthorizationFilter
    {
        private BaseService service;
        private Role role;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        /// <param name="role">默认仅要求登录</param>
        public NeedLogon(BaseService service, Role role = Role.Logon)
        {
            this.service = service;
            this.role = role;
        }

        public void OnAuthorization(AuthorizationFilterContext context)
        {
            if (!service.HasRole(role))
            {
                switch (role)
                {
                    case Role.Logon:
                        context.Result = new RedirectResult("/?logon=true");
                        break;
                    case Role.Contact:
                        context.Result = new RedirectResult("/User/Homepage");
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
